package com.gcb.common.utils.huawei;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.gcb.common.exception.ServiceException;
import com.gcb.common.utils.huawei.vo.BusinessLicenseResultVO;
import com.gcb.common.utils.huawei.vo.IdCardResultVO;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.ocr.v1.OcrClient;
import com.huaweicloud.sdk.ocr.v1.model.BusinessLicenseRequestBody;
import com.huaweicloud.sdk.ocr.v1.model.BusinessLicenseResult;
import com.huaweicloud.sdk.ocr.v1.model.IdCardRequestBody;
import com.huaweicloud.sdk.ocr.v1.model.IdCardResult;
import com.huaweicloud.sdk.ocr.v1.model.RecognizeBusinessLicenseRequest;
import com.huaweicloud.sdk.ocr.v1.model.RecognizeBusinessLicenseResponse;
import com.huaweicloud.sdk.ocr.v1.model.RecognizeIdCardRequest;
import com.huaweicloud.sdk.ocr.v1.model.RecognizeIdCardResponse;
import com.huaweicloud.sdk.ocr.v1.region.OcrRegion;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * OCR工具类
 */
@Slf4j
@Component
public class OcrUtils {

    /**
     * 身份证识别 图片url
     *
     * @param url
     * @param side
     * @return
     */
    public static IdCardResultVO idCardOcrWithUrl(String url, String side) {
        ICredential auth = new BasicCredentials()
                .withAk(HuaweiConstant.AK)
                .withSk(HuaweiConstant.SK);
        OcrClient client = OcrClient.newBuilder()
                .withCredential(auth)
                .withRegion(OcrRegion.valueOf(HuaweiConstant.REGION))
                .build();
        RecognizeIdCardRequest request = new RecognizeIdCardRequest();
        IdCardRequestBody body = new IdCardRequestBody();
        body.withDetectCopy(true);
        body.withDetectReproduce(true);
        body.withReturnTextLocation(true);
        body.withReturnVerification(true);
        if (StrUtil.isNotBlank(side)) {
            body.withSide(side);
        }
        body.withUrl(url);
        request.withBody(body);
        try {
            RecognizeIdCardResponse response = client.recognizeIdCard(request);
            log.info("调用华为云身份证识别接口成功，返回结果：{}", response.toString());
            IdCardResult result = response.getResult();
            IdCardResultVO vo = new IdCardResultVO();
            BeanUtil.copyProperties(result, vo);
            return vo;
        } catch (Exception e) {
            log.error("调用华为云身份证识别接口失败，错误信息：{}", e.getMessage());
            e.printStackTrace();
            throw new ServiceException("调用华为云身份证识别接口失败");
        }
    }

    /**
     * 营业执照识别 图片url
     *
     * @param url
     */
    public static BusinessLicenseResultVO businessLicenseOcrWithUrl(String url) {
        ICredential auth = new BasicCredentials()
                .withAk(HuaweiConstant.AK)
                .withSk(HuaweiConstant.SK);
        OcrClient client = OcrClient.newBuilder()
                .withCredential(auth)
                .withRegion(OcrRegion.valueOf(HuaweiConstant.REGION))
                .build();
        RecognizeBusinessLicenseRequest request = new RecognizeBusinessLicenseRequest();
        BusinessLicenseRequestBody body = new BusinessLicenseRequestBody();
        body.withUrl(url);
        request.withBody(body);
        try {
            RecognizeBusinessLicenseResponse response = client.recognizeBusinessLicense(request);
            log.info("调用华为云营业执照识别接口成功，返回结果：{}", response.toString());
            BusinessLicenseResult result = response.getResult();
            BusinessLicenseResultVO vo = new BusinessLicenseResultVO();
            BeanUtil.copyProperties(result, vo);
            return vo;
        } catch (Exception e) {
            log.error("调用华为云营业执照识别接口失败，错误信息：{}", e.getMessage());
            e.printStackTrace();
            throw new ServiceException("调用华为云营业执照识别接口失败");
        }
    }

}
